package com.lv.leyou.web;

import com.lv.leyou.common.dto.PageDTO;
import com.lv.leyou.dto.SkuDTO;
import com.lv.leyou.dto.SpecParamDTO;
import com.lv.leyou.dto.SpuDTO;
import com.lv.leyou.dto.SpuDetailDTO;
import com.lv.leyou.service.SpuDetailService;
import com.lv.leyou.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;

    @GetMapping("/spu/page")
    //分页条件查询
    public ResponseEntity<PageDTO<SpuDTO>> pageSpu(@RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                                   @RequestParam(value = "rows", required = false, defaultValue = "5") Long rows,
                                                   @RequestParam(value = "brandId", required = false) Long brandId,
                                                   @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                   @RequestParam(value = "id", required = false) Long id,
                                                   @RequestParam(value = "saleable", required = false) Boolean saleable) {


        return ResponseEntity.ok(spuService.pageSpu(page, rows, brandId, categoryId, id, saleable));
    }

    //商品新增
    @PostMapping("/spu")
    public ResponseEntity<Void> addSpu(@RequestBody SpuDTO spuDTO) {
        this.spuService.addSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据id查询商品
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuDto(@PathVariable("id") Long id) {

        return ResponseEntity.ok(this.spuService.querySpuDto(id));
    }

    //根据id查询SpuDetail
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpecDetail(@RequestParam("id") Long id) {
        return ResponseEntity.ok(this.spuService.querySpecDetail(id));
    }

    //根据Spuid查询SKU集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkulistBySpuid(@RequestParam("id") Long id) {
        return ResponseEntity.ok(this.spuService.querySkulistBySpuid(id));
    }

    //根据id集合查询Sku集合
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuList(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(this.spuService.querySkuList(ids));
    }

    //根据spuid查询spu的所有规格参数键值对。
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(@RequestParam("id") Long id,
                                                              @RequestParam(value = "searching", required = false) Boolean searching) {

        return ResponseEntity.ok(this.spuDetailService.querySpecParams(id, searching));
    }

    //根据id查询spu
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySkuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.spuService.querySkuById(id));
    }

    //商品的上下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> updatesaleable(@RequestParam(value = "id", required = false) Long id,
                                               @RequestParam(value = "saleable", required = false) Boolean saleable) {
        spuService.updatesaleable(id, saleable);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
